//
//  CustomScrollViewController.m
//  ScrollViews
//
//  Created by cygarm on 15/6/7.
//  Copyright (c) 2015年 cygarm. All rights reserved.
//

#import "CustomScrollViewController.h"

@interface CustomScrollViewController ()

@end

@implementation CustomScrollViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    // 设置container view来保持你定制的视图层次
    CGSize containerSize = CGSizeMake(640.0, 640.0);
    containerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, containerSize.width, containerSize.height)];
    [[self scrollView]addSubview:containerView];
    
    // 设置你定制的视图层次
    UIView *redView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 640, 80)];
    redView.backgroundColor = [UIColor redColor];
    [containerView addSubview:redView];

    
    UIView *blueView = [[UIView alloc]initWithFrame:CGRectMake(0, 560, 640, 80)];
    blueView.backgroundColor = [UIColor blueColor];
    [containerView addSubview:blueView];

    
    UIView *greenView = [[UIView alloc]initWithFrame:CGRectMake(160, 160, 320, 320)];
    greenView.backgroundColor = [UIColor greenColor];
    [containerView addSubview:greenView];

    
    UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"slow.png"]];
    imageView.center = CGPointMake(320, 320);
    [containerView addSubview:imageView];
    
    // 告诉scroll view内容的尺寸
    self.scrollView.contentSize = containerSize;
    
    // 设置最大和最小的缩放系数
    CGRect scrollViewFrame = self.scrollView.frame;
    CGFloat scaleWidth = scrollViewFrame.size.width / self.scrollView.contentSize.width;
    CGFloat scaleHeight = scrollViewFrame.size.height / self.scrollView.contentSize.height;

    CGFloat minScale = MIN(scaleWidth, scaleHeight);

//
    self.scrollView.minimumZoomScale = minScale;
    self.scrollView.maximumZoomScale = 1.0;
    self.scrollView.zoomScale = 1.0;
    
    [self centerScrollViewContents];

}

- (void) centerScrollViewContents {
    CGSize boundsSize = self.scrollView.bounds.size;
    CGRect contentsFrame = containerView.frame;
    
    containerView.frame = contentsFrame;
    
    if (contentsFrame.size.width < boundsSize.width) {
        contentsFrame.origin.x = (boundsSize.width - contentsFrame.size.width) / 2.0;
    } else {
        contentsFrame.origin.x = 0.0;
    }
    
    if (contentsFrame.size.height < boundsSize.height) {
        contentsFrame.origin.y = (boundsSize.height - contentsFrame.size.height) / 2.0;
    } else {
        contentsFrame.origin.y = 0.0;
    }
    
    containerView.frame = contentsFrame;
}

-(UIView *) viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return containerView;
}

-(void) scrollViewDidZoom:(UIScrollView *)scrollView {
    [self centerScrollViewContents];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
